package com.test.webapp.listener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.test.Constants;
import com.test.util.ConfigUtils;

/**
 * <p>
 * StartupListener class used to initialize and database settings and populate
 * any application-wide drop-downs.
 * <p>
 * Keep in mind that this listener is executed outside of
 * OpenSessionInViewFilter, so if you're using Hibernate you'll have to
 * explicitly initialize all loaded data at the Dao or service level to avoid
 * LazyInitializationException. Hibernate.initialize() works well for doing
 * this.
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 * @web.listener
 */
public class StartupListener extends ContextLoaderListener implements
		ServletContextListener {

	private static final Log log = LogFactory.getLog(StartupListener.class);

	@SuppressWarnings("unchecked")
	@Override
	public void contextInitialized(ServletContextEvent event) {
		if (log.isDebugEnabled()) {
			log.debug("initializing context...");
		}

		try {
			log.info("加载settings的配置信息...START");
			// 读取配置文件信息
			ConfigUtils settingsConfig = new ConfigUtils("settings.properties",
					ConfigUtils.TYPE.PROPERTY);
			Constants.settings = settingsConfig.getAllValue();

			List<String> categoryIds = (List<String>) Constants.settings
					.get("IPAD_CATEGORYIDS");
			if (categoryIds != null && !categoryIds.isEmpty()) {
				for (String categoryId : categoryIds) {
					Constants.IPAD_CATEGORY_IDS.put(categoryId, categoryId);
				}
			}

			log.info("加载settings的配置信息...END");
			log.info("加载ICT_PATH的配置信息...START");
			// GlobleInitAnalysis globleAnalysis =
			// GlobleInitAnalysis.getInstance();
			// globleAnalysis.initGlobleData("ICT_PATH.properties",
			// ConfigUtils.TYPE.PROPERTY, null);
			// log.info("加载ICT_PATH的配置信息...END");
			// log.info("启动定时处理器...START");
			//
			// log.info("启动定时处理器...END");
			// log.info("加载LUCENE路径的配置信息...START");
			// ConfigUtils lucenePathConfig = new
			// ConfigUtils("LUCENE_PATH.properties", ConfigUtils.TYPE.PROPERTY);
			// Constants.lucenePath = lucenePathConfig.getAllValue();
			log.info("加载LUCENE路径的配置信息...END");
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// call Spring's context ContextLoaderListener to initialize
		// all the context files specified in web.xml
		super.contextInitialized(event);

		ServletContext context = event.getServletContext();

		// Orion starts Servlets before Listeners, so check if the config
		// object already exists
		Map config = (HashMap) context.getAttribute((String) Constants.settings
				.get("CONFIG"));

		if (config == null) {
			config = new HashMap();
		}

		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(context);

		Constants.ctx = ctx;

	}

}
